package org.example.system.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import org.example.system.entity.Post;
import org.example.system.entity.Star;
import org.example.system.entity.User;
import org.example.system.mapper.StarMapper;
import org.example.system.service.StarService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.Date;

@Service
public class StarServiceImpl implements StarService {
    @Autowired
    private StarMapper starMapper;

    /**
     * 按条件分页查询
     * 影响条件有收藏标题
     * @param star
     * @return
     */
    @Override
    public IPage<Star> selectPage(Star star){
        LambdaQueryWrapper<Star> wrapper = new LambdaQueryWrapper<>();
        System.out.println(star);
        //条件
        if (star.getId() != null){
            wrapper.eq(Star:: getId,star.getId());
        }
        if (star.getPostId() != null){
            wrapper.eq(Star:: getPostId,star.getPostId());
        }
        if (star.getUserId() != null){
            wrapper.eq(Star:: getUserId,star.getUserId());
        }
        IPage<Star> page = new Page<>();
        page.setCurrent(star.getPageNum());
        page.setSize(star.getPageSize());
        return starMapper.selectPage(page,wrapper);
    }

    /**
     * 按分页查询联查用户表
     * @param star
     * @return
     */
    @Override
    public IPage<Star> select(Star star){
        MPJLambdaWrapper<Star> wrapper = new MPJLambdaWrapper<>();
        wrapper.orderByDesc(Star::getId);
        IPage<Star> page = new Page<>();
        page.setCurrent(star.getPageNum());
        page.setSize(star.getPageSize());
        return starMapper.selectPage(page,wrapper);
    }

    /**
     * 添加收藏
     * @param star
     * @return
     */
    @Override
    public int add(Star star) {
        star.setUpdateTime(new Date());
        star.setNote("收藏创建成功！");
        return starMapper.insert(star);
    }

    /**
     * 修改
     * @param star
     * @return
     */
    @Override
    public int update(Star star) {
        star.setUpdateTime(new Date());
        star.setNote("修改收藏数据");
        return starMapper.updateById(star);
    }

    /**
     * 删除
     * @param ids
     * @return
     */
    @Override
    public int deleteBatch(Long[] ids) {
        return starMapper.deleteBatchIds(Arrays.asList(ids));
    }

    /**
     * 根据id获取信息
     * @param id
     * @return
     */
    @Override
    public Star getOne(Long id) {
        return starMapper.selectById(id);
    }
}
